package com.xiaohu.myspace.database;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;

import javax.sql.DataSource;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import com.xiaohu.myspace.database.entity.BaseEntity;

public class MyHibernateDaoSupport<T extends BaseEntity> {

	protected boolean insertEntity(T t, Session session) {
		try {
			Serializable s = session.save(t);
			if (null == s) {
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
		}
	}

	protected boolean deleteEntity(T t, Session session) {
		try {
			session.delete(t);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
		}
	}

	protected boolean updateEntity(T t, Session session) {
		try {
			session.update(t);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
		}
	}

	@SuppressWarnings("unchecked")
	protected T getObjectWithHql(String hql, Session session) {
		try {
			ArrayList<T> arraylist = (ArrayList<T>) session.createQuery(hql)
					.list();
			if (null != arraylist && arraylist.size() > 0) {
				return arraylist.get(0);
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
		}
	}

	@SuppressWarnings("unchecked")
	protected ArrayList<T> getAllObjectWithHql(String hql, Session session) {
		try {
			ArrayList<T> arrayList = (ArrayList<T>) session.createQuery(hql)
					.list();
			return arrayList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
		}
	}

	protected boolean executeHql(String hql, Session session,
			Map<String, Object> params) {
		try {
			SQLQuery sqlquery = session.createSQLQuery(hql);
			if (params != null) {
				sqlquery.setProperties(params);// 传值
			}
			int index = sqlquery.executeUpdate();
			return index > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
		}
	}

	protected boolean executeSql(String sql, SessionFactory sessionFactory) {
		boolean flag = false;
		DataSource dataSource = SessionFactoryUtils
				.getDataSource(sessionFactory);
		try {
			Statement statement = dataSource.getConnection().createStatement();
			flag = statement.execute(sql);
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		} finally {
		}
		return flag;

	}

	protected ResultSet getAllDataWithSql(String sql,
			SessionFactory sessionFactory) {
		ResultSet resultSet = null;
		DataSource dataSource = SessionFactoryUtils
				.getDataSource(sessionFactory);
		try {
			Statement statement = dataSource.getConnection().createStatement();
			resultSet = statement.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultSet;

	}

}
